Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for triple-channel INA3221 i2c voltage-and-current sensor #15091

Merged
merged 7 commits into from
Dec 2, 2024

Conversation

peterbarker
Copy link
Contributor

This adds support for a new i2c-based voltage/current sensor.

This driver is not currently suitable for merging. It might be that this is more of a component than an e.g. battery monitor in-and-of itself. It might form a base class for a product based around it, perhaps (as several of the smbus-based devices are based around a "generic" driver). I'm waiting on more design details to see where it goes from here.

This has been tested on a CJMCU-3221 breakout board.

Of interest here is the i2c simulator for the device - we're still fleshing out how simulated i2c devices work, but the "writable registers" mask is interesting.

This work is being sponsored by Wurzbach Electronics.

@apache405
Copy link

YAY!

Thanks for taking on the task of getting this driver built. I'm gonna have a lot of fun putting this into new stuff.

@tpwrules
Copy link
Contributor

Tested that voltage, current, and accumulation now work properly with real hardware on stampfly.

Please feel free to rebase/squash as desired, though I would like credit for both of us to be preserved.

The sim needs fixup to use the scaling values from the driver, and possibly modernization?

@peterbarker peterbarker marked this pull request as ready for review November 26, 2024 03:48
@tpwrules tpwrules removed the WIP label Nov 29, 2024
@tpwrules
Copy link
Contributor

Okay, this should be ready to go! Tests now do something useful too.

Copy link
Contributor

@srmainwaring srmainwaring left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. I have flown the stampfly branch containing these changes with ELRS telem and the reported voltage and current seem reasonable.

I have not gone through the driver in detail (comparison with datasheet etc.). If you'd like that carried out as part of the review pls ltm.

Have a few suggestions:

  • increase test timeout so it completes without needing to enable speedup
  • add further comments on the hardcoded test values in test and simulated sensor

tstart = self.get_sim_time()
while not (seen_1 and seen_3):
m = self.assert_receive_message('BATTERY_STATUS')
if self.get_sim_time() - tstart > 10:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

10s results in a timeout when running at standard speed on a mac M1. With --speedup 2 the test completes. Not sure what the policy is for autotest (do we assume that in CI they'll be run with a speedup?). Increasing to 15 passes.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is strange to hear, I chose 10 seconds as a "couldn't possibly fail" value. There might be an inherent test timeout too which would remove the need to check, but I'm not 100% sure? Also get_sim_time should be scaled with speedup.

How are you running the test? Using Tools/autotest/autotest.py build.Sub test.Sub.INA3221 --no-clean and a print statement the total elapsed "time" is well under 1 second on my x86 machine and passes every time. I tested using --speedup=1, --speedup=2, and whatever the autotest script default is.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disregard - I was using a --debug build and that explains the timeout.

@tridge
Copy link
Contributor

tridge commented Dec 1, 2024

@tpwrules @peterbarker this PR looks good to me, just needs some commits squashing

* correctly validate channel parameter and improve other parameter
access

* dynamically enable channels to avoid spending time converting unused
channels

* implement tracking of reading health

* correct reading scaling by using datasheet values

* accumulate measured current to track used mAh and Wh

* make configurable using #defines (and hwdef) for integrators

* correctly separate and lock frontend and backend state. Note that
_state of frontend can only be accessed in `read()` method.
* make test actually test something

* fix scaling to match datasheet values
* make test actually test something

* fix scaling to match datasheet values
@tpwrules tpwrules merged commit da4fee5 into ArduPilot:master Dec 2, 2024
99 checks passed
@peterbarker peterbarker deleted the pr/ina3221 branch December 2, 2024 19:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
WikiNeeded needs wiki update
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants